<?php
declare (strict_types=1);

namespace app\topic\service;

use app\common\service\BaseService;
use app\topic\logic\Topic as TopicLogic;
use think\db\exception\DbException;
use think\facade\Event;
use think\facade\Request;
use think\Response;

/**
 * 资讯管理
 */
class Topic extends BaseService
{

    /**
     * 资讯首页 - PC
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date：   2020-06-30
     */
    public function index()
    {
        $map = [];

        $mhm_id = request()->mhm_id;
        if (input('?get.mhm_id') && input('get.mhm_id/d')) {
            $mhm_id = input('get.mhm_id/d');
        }
        $map[] = ['mhm_id', '=', $mhm_id];

        if (input('?get.category') && input('get.category/d')) {
            $map[] = ['category', 'LIKE', '%,' . input('get.category/d') . ",%"];
        }
        $data = app(TopicLogic::class)->index($map, $mhm_id);

        return $this->parseToData($data, 1, '获取成功');
    }

    /** 资讯列表 - APP
     *
     * @throws DbException
     */
    public function list()
    {
        $map = [];

        $mhm_id = request()->mhm_id;
        if (input('?get.mhm_id') && input('get.mhm_id/d')) {
            $mhm_id = input('get.mhm_id/d');
        }
        $map[] = ['mhm_id', '=', $mhm_id];

        if (input('?get.category') && input('get.category/d')) {
            $map[] = ['category', 'LIKE', '%,' . input('get.category/d') . ",%"];
        }

        $data = app(TopicLogic::class)->list($map);

        return $this->parseToData($data, 1, '获取成功');
    }

    /**
     * @throws DbException
     */
    public function fetch(int $id)
    {
        $data = app(TopicLogic::class)->fetch($id);

        Event::trigger('WebsiteVisit', [
            'mhm_id'        => $data['detail']->mhm_id,
            'resource_type' => 'topic',
            'resource_id'   => $id,
        ]);

        return $this->parseToData($data, 1, '获取成功');
    }

    public function content(int $id)
    {
        return app(TopicLogic::class)->content($id);
    }

    /**
     * @throws DbException
     */
    public function getComments(int $id)
    {
        $data = app(TopicLogic::class)->comments($id);

        return $this->parseToData($data, 1, '获取成功');
    }

    public function postComment(int $id)
    {
        $content = Request::post('content');
        $data    = app(TopicLogic::class)->postComment($id, $content);

        return $this->parseToData($data, 1, '评论成功');
    }

    /**
     * delComment
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 23/09/25
     *
     * @param int $id
     * @return Response
     */
    public function delComment(int $id)
    {
        app(TopicLogic::class)->delComment($id);
        return $this->parseToData(null, 1, '删除成功');
    }

    /** 点赞状态 */
    public function updateCommentLikeStatus(int $id)
    {
        $status = Request::put('status/d');
        $re     = app(TopicLogic::class)->updateCommentLikeStatus($id, $status);

        return $this->parseToData($re, 1, '成功');
    }

    /** 评论回复点赞状态 */
    public function updateReplyLikeStatus(int $id)
    {
        $status = Request::put('status/d');
        $re     = app(TopicLogic::class)->updateReplyLikeStatus($id, $status);

        return $this->parseToData($re, 1, '成功');
    }

    public function addCommentReply(int $id)
    {
        $param = Request::post();
        $re    = app(TopicLogic::class)->addCommentReply($id, $param);

        return $this->parseToData($re, 1, '回复成功');
    }

    /**
     * @throws DbException
     */
    public function getCommentReply(int $id)
    {
        $reply = app(TopicLogic::class)->getCommentReply($id);

        return $this->parseToData($reply, 1, '获取成功');
    }

    public function delCommentReply(int $id)
    {
        app(TopicLogic::class)->delCommentReply($id);
        return $this->parseToData(null, 1, '删除成功');
    }
}
